package com.ciwong.epaper.modules.homework.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.ciwong.epaper.modules.homework.ui.BeiKeFragment;
import com.ciwong.epaper.modules.homework.ui.ClassWorkFragment;
import com.ciwong.epaper.modules.homework.ui.DkWorkFragment;
import com.ciwong.epaper.modules.homework.ui.TeacherWorkFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: wyk
 * @time: 2021/9/9 17:19
 */
public class WorkContentFragmentAdapter extends FragmentStatePagerAdapter {

    private List<String> names;

    public WorkContentFragmentAdapter(FragmentManager fm) {
        super(fm);
        this.names = new ArrayList<>();
    }

    /**
     * 数据列表
     *
     * @param datas
     */
    public void setList(List<String> datas) {
        this.names.clear();
        this.names.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        Bundle bundle = new Bundle();
        Fragment fragment = new Fragment();
        switch (position){
            case 0:
                fragment = new TeacherWorkFragment();
                bundle.putString("name", names.get(position));
                bundle.putInt("workStatus",3);
                fragment.setArguments(bundle);
                break;
            case 1:
                fragment = new TeacherWorkFragment();
                bundle.putString("name", names.get(position));
                bundle.putInt("workStatus",2);
                fragment.setArguments(bundle);
                break;
            case 2:
                fragment = new ClassWorkFragment();
                bundle.putString("name", names.get(position));
                fragment.setArguments(bundle);
                break;
            case 3:
                fragment = new DkWorkFragment();
                bundle.putString("name", names.get(position));
                fragment.setArguments(bundle);
                break;
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return names.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String plateName = names.get(position);
        if (plateName == null) {
            plateName = "";
        } else if (plateName.length() > 15) {
            plateName = plateName.substring(0, 15) + "...";
        }
        return plateName;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //super.destroyItem(container, position, object);
    }

}
